//
//  complicationChooseController.m
//  QuickCure1
//
//  Created by 冬冬 on 2016/11/2.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "complicationChooseController.h"
#import "complicationChooseCell.h"
#import "ComplicationModel.h"
#import "NetWorkTool.h"
#import <MJRefresh.h>

@interface complicationChooseController ()<UITableViewDelegate,UITableViewDataSource>

//记录
@property (nonatomic, weak) UITableView *tableView;

//并发症集合
@property (nonatomic, strong) NSMutableArray *illArray;



@end

@implementation complicationChooseController

static NSString *cellId = @"cellId";

-(NSMutableArray *)illArray{
    
    if(!_illArray){
        _illArray = [NSMutableArray array];
    }
    return _illArray;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"并发症选择";
    UIBarButtonItem *rightbtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightBtn:)];
    rightbtn.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightbtn;
    
    
    //创建tableView
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 40;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册
    [tableView registerNib:[UINib nibWithNibName:@"complicationChooseCell" bundle:nil] forCellReuseIdentifier:cellId];
    self.tableView = tableView;

    //下拉刷新
    [self pullRefresh];
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    complicationChooseCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
//    NSArray *arr = @[@"缺铁性贫血x",@"胃穿孔",@"呕血",@"黑粪",@"未特指的胃肠出血",@"其他继发性肿瘤"];
    
    ComplicationModel *model =self.dataArr[indexPath.row];
    cell.lbl.text = model.complication_name;
    
    cell.lbl.font = MYROUNDFONT(15);
    
    cell.arr = self.illArray;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    complicationChooseCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //在点击cell中掉cell.点击btn的方法,使点击cell相当于点击btn的效果.
    [cell clickBtn:cell.btn];
    [cell setSelected:NO animated:NO];

}

//点击右上角的btn
-(void)clickRightBtn:(UIBarButtonItem *)sender{
    NSInteger rowNum = [_tableView numberOfRowsInSection:0];
    for (int i = 0; i < rowNum; i++) {
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
        complicationChooseCell *cell = [_tableView cellForRowAtIndexPath:indexpath];
     
        
        if (cell.illName&&![self.illArray containsObject:cell.illName]) {
           [self.illArray addObject:cell.illName];
        }
        
    }
    
//    NSLog(@"%@",self.illArray);
    
    //发通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"complicationChoose" object:self.illArray userInfo:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark - 下拉刷新
-(void)pullRefresh{
    
    
    
    __weak complicationChooseController *weakSelf = self;
    
    NSString *urlString = kComplicationListUrl;
    NSDictionary *paramDic = @{@"page":@(self.dataNum),@"page_size":@15,@"ci2_id":@3
                               };
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(NSDictionary* responseObject) {

            NSArray *arr = responseObject[@"data"];
            weakSelf.dataArr = [NSArray yy_modelArrayWithClass:[ComplicationModel class] json:arr];
            [weakSelf.tableView reloadData];
            
            [weakSelf.tableView.mj_header endRefreshing];
            
        } failureBlock:^(NSError *error) {
            
            
        }];
    }];
    
    
    MJRefreshNormalHeader *header = (MJRefreshNormalHeader *)self.tableView.mj_header;
    
    [header setTitle:@"我正在刷新.." forState:MJRefreshStateRefreshing];
    [header setTitle:@"松手就可以刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"刷新完毕" forState:MJRefreshStateNoMoreData];
    
    header.lastUpdatedTimeText = ^(NSDate *date){
        return @"";
    };
//    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
}





-(void)dealloc{
    NSLog(@"complicationChooseController释放");
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end









